package cheeese.and.crackers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapViewActivity extends MapActivity{
	
	private MapView map;
	private UserList users;
	private User activeUser;
	private Task activeTask;

	public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.mapview);
    	map = (MapView)findViewById(R.id.map);
    	
    	users = new UserList();
        try {
			users.load(getApplicationContext());
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), (CharSequence)("Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
    		try {
    			Thread.sleep(2000);
    		} 
    		catch(InterruptedException e2) {} 
    		android.os.Process.killProcess(android.os.Process.myPid());
		}
   	//Get Active User
		Bundle activeUserBundle = this.getIntent().getExtras();
		String activeUserString = activeUserBundle.getString("ACTIVE_USER");
		activeUser = users.getUser(activeUserString);
		String activeTaskString = activeUserBundle.getString("ACTIVE_TASK");
		activeTask = activeUser.getTasks().getTask(activeTaskString);
    	
    	Geocoder myGeocoder = new Geocoder(this, Locale.getDefault());
 		List<Address> foundGeocode = null;
 		try {
			foundGeocode = myGeocoder.getFromLocationName(activeTask.getLocation(), 1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Address location = foundGeocode.get(0);
		
		GeoPoint geoLocation = new GeoPoint((int)(location.getLatitude() * 1E6), (int)(location.getLongitude() * 1E6));
		
		map.getController().animateTo(geoLocation);
		map.setClickable(true);
		map.setBuiltInZoomControls(true);
		
		OverlayItem overlayItem = new OverlayItem(geoLocation, activeTask.getLocation(), null);
		
		List<Overlay> mapOverlays = map.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.megustaclear);
		LocationOverlay myOverlay = new LocationOverlay(drawable);
		myOverlay.addOverlay(overlayItem);
		mapOverlays.add(myOverlay);
		
		map.getController().animateTo(geoLocation);
    	
	}
	
	public class LocationOverlay extends ItemizedOverlay{
  	  
  	  private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

		public LocationOverlay(Drawable defaultMarker) {
			super(boundCenterBottom(defaultMarker));
		}
		
		public void addOverlay(OverlayItem overlay){
			mapOverlays.add(overlay);
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return mapOverlays.get(i);
		}

		@Override
		public int size() {
			return mapOverlays.size();
		}
  	  
    }
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
